﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using KL.Store.Web.Models;
using LM = KL.Store.Core.Localization;
using KL.Store.Core;

namespace KL.Store.Web.Controllers
{
    public class ErrorController : KL.Store.Core.Web.BaseController
    {
        public ActionResult Index(ErrorModel model)
        {
            model.ErrorHeader = LM.Current.Get(eStoreUser.CultureInfo, "Beklenmeyen bir hata oluştu!");
            model.ErrorText = "";
            try
            {
                if (Session["###eStoreError"] != null)
                {
                    Exception ex = ((Exception)Session["###eStoreError"]);
                    model.ErrorText = ex.ToString();

                    if (!string.IsNullOrEmpty(Parameters.ErrorMessageToAddress) && !string.IsNullOrEmpty(model.ErrorText))
                    {
                        try
                        {
                            Utils.SendMail(Parameters.ErrorMessageToAddress, "", "", "Beklenmeyen Hata", model.ErrorText.Replace(":line ", "<b>:line </b>"), true);
                        }
                        catch
                        {

                        }
                    }

                    #region AccountId

                    int AccountId = -1;
                    if (eStoreUser != null)
                    {
                        AccountId = eStoreUser.AccountId;
                    }

                    #endregion

                    Logger.Write(ex, "Exception", AccountId);
                }
            }
            catch (Exception ex)
            {
                model.ErrorText = ex.ToString();
            }

            return View(model);
        }
    }
}